*
* This file contains information on how the ITERATE terrorism variables were prepared for analysis 
* 
* One issue is that in most instances origin and destination of attack are same country.
* So instead of using the origin as originally coded, we assume that the origin comes 
* from "first nationality" of attackers.
*
* Also, in data, each observation is an attack in a given year. So we need to aggregate on all 
* attacks coming from a given country, and targeting a given country, for each year in the data.
* Note: data is in long format, with year as a variable.
* 
gen oldorigin=origin
la var oldorigin "Origin as coded in original data"
replace origin=fitnationality if origin==destination
la var origin "Fitnationality in original data if origin==destination"
*
* 
by year origin, sort: egen tattacksfrom=count(origin)				/* Count observations in sub-cat to get # of attacks */
by year destination, sort: egen tattacksto=count(origin)
la var tattacksfrom "Tot # of attacks emanating from origin"
la var tattacksto "Tot # of attacks to destination"
*
mvdecode _all, mv(997/999)								/* Set missing values */
*
* Also want to aggregate on dyads to compute total nums of attacks from an 
* origin to a destination. 
*
by year origin destination, sort: egen tattfromto=count(origin)		/* # of attacks from origin to destination */
la var tattfromto "Tot # of attacks from origin to destination (dyad)"
*
*
by year origin destination, sort: keep if _n==1					/* Keep only one observation per dyad-year */
*
* Also want to computer cumulative # of attacks; this is a bit tricker
* Origin countries can appear more than once in a year if more than one destination, so to get cumulative # of attacks
* originating from a country (regardless of target), use just one observation for each year (_n=1).  
*
foreach var of varlist  tattacksfrom   {
  capture drop c`var'
  gen c`var'=`var'
  by year origin, sort: replace c`var'=0 if _n>1		/* Zero out 2 through nth observations for a given origin */			
*
  gsort origin year -c`var'								/* Sort so that non-zero values appear first */
  by origin, sort: replace c`var'=sum(c`var')			/* Generate cumulative nums */
}
foreach var of varlist tattacksto  {
  capture drop c`var'
  gen c`var'=`var'
  by year destination, sort: replace c`var'=0 if _n>1
*
  gsort destination year -c`var'						
  by destination, sort: replace c`var'=sum(c`var')
}
*
* For dyad-level computation no zeroing-out necessary because each observation a unique dyad-year. 
* 

foreach var of varlist  tattfromto {
  capture drop c`var'
  sort origin destination year
  by origin destination, sort: gen c`var'=sum(`var')
}
*
la var ctattacksto "Cumulative # of attacks to destination"
la var ctattacksfrom "Cumulative # of attacks from origin" 
la var ctattfromto "Cumulative # of attacks from origin to destination"
*
fillin origin destination year							/* Add observations (w/missing vals) for missing years */
replace tattfromto=0 if tattfromto==.						/* Where tattfromto missing, replace with 0 */
egen idfill=group(origin destination)
by idfill, sort: egen temp=sum(_fillin)					      /* Figure out how many added observations 4 each dyad */
drop if temp==28										/* Drop dyad if all observations in it added with fillin */
*
* Had to do "net search vallist" to download the command vallist
* vallist lists the unique values of a variable
*
capture drop yearcoef2007 yearcoef2000
gen yearcoef2007=.
gen yearcoef2000=.
qui vallist idfill, global(idlist)			
sort origin destination year
foreach num of global idlist {							
   qui reg tattfromto year if year>=1990 & idfill==`num'			/* Run a regression on for each dyad (across years) */
   qui replace yearcoef2007=_b[year] if idfill==`num'				/* Save coefficient on year into each dyad's observations */
   qui reg tattfromto year if year>=1990 & year<=2000 & idfill==`num'	/* Run a regression on for each dyad (across years) */
   qui replace yearcoef2000=_b[year] if idfill==`num'				/* Save coefficient on year into each dyad's observations */
}
la var yearcoef2007 "Coef on regression of tattfromto on year, year: 1990-2007"
la var yearcoef2000 "Coef on regression of tattfromto on year, year: 1990-2000"
drop if _fillin==1									/* Don't need extra observations now */
capture drop trend2007
gen trend2007=.											/* Record trend in variable (since 1990) */
capture drop trend2000
gen trend2000=.	
replace trend2007=-1 if yearcoef2007<0
replace trend2007=0 if yearcoef2007==0
replace trend2007=1 if yearcoef2007>0
la var trend2007 "1 if upward trend in tattfromto betw 1990 and 2000, 0 for neutral, -1 for negative"
replace trend2000=-1 if yearcoef2000<0
replace trend2000=0 if yearcoef2000==0
replace trend2000=1 if yearcoef2000>0
la var trend2000 "1 if upward trend in tattfromto betw 1990 and 2000, 0 for neutral, -1 for negative"
*
drop _fillin
drop idfill
*
* Now must convert from long to wide format. 
*
egen id=group(origin destination)
drop if id==.
drop  numnationalities fitnationality secondnationality thirdnationality wounded killed oldorigin
reshape wide tattacksfrom-ctwoufromto, i(id) j(year)				/* yearcoef and trend are constant across years */

* 
*
* Not all dyads have data for all years, so reshape gives the missing years missing values.
* 
* 
local stubs "tattacksto ctattacksto"
foreach var of local stubs {
  forval num = 1980/2007 {
    tempvar temp
    qui by destination, sort: egen `temp'=mean(`var'`num')
    qui replace `var'`num'=`temp' 
  }
}
*
local stubs "tattacksfrom ctattacksfrom"
foreach var of local stubs {
  forval num = 1980/2007 {
    tempvar temp
    qui by origin, sort: egen `temp'=mean(`var'`num')
    qui replace `var'`num'=`temp' 
  }
}
*
 local stubs "tattacksfrom tattacksto tattfromto"
 foreach var of local stubs {
  forval num = 1980/2007 {
    qui replace `var'`num'=0 if `var'`num'==.
  }
}
*
* For cumulative variables things are more complicated because the missing values in a given year
* must be replaced by the largest value that has occurred from all the years preceding.
* 
order id origin destination ctattacksfrom* ctattacksto* ctattfromto*
*
local stubs "ctattacksfrom ctattacksto ctattfromto"
foreach var of local stubs {
forval num = 1980/2007 {
   tempvar temp
   egen `temp'=rowmax(`var'1980-`var'`num')		      /* Compute max value of cumulative vars preceding year `num' */
   qui replace `var'`num'=`temp' if `var'`num'==.	      /* Replace missing cumulative values */
   qui replace `var'`num'=0 if `var'`num'==.			/* If all missing, then replace with 0 */
} 
}

replace yearcoef2007=0 if yearcoef2007==. 					/* If no terror attacks in dyad, set yearcoef to 0 */
replace trend2007=0 if trend2007==. 						/* If no terror attacks in dyad, set trend to 0 */
replace yearcoef2000=0 if yearcoef2000==. 					/* If no terror attacks in dyad, set yearcoef to 0 */
replace trend2000=0 if trend2000==. 						/* If no terror attacks in dyad, set trend to 0 */

recode trend2000 ( -1 0 = 0) (1=1), gen(trend2000b)
la var trend2000b "0 if non-positive trend, 1 otherwise"


